package gsp420.helloworld;

import java.util.Vector;

public class Prime {
	/** all found primes stored here */
	public Vector<Integer> primes = new Vector<Integer>();
	
	/** determines if the given number is prime by checking its modulos remainder against all other known primes */
	public boolean isPrime(int number){
		int halfNum = number/2, test;
		for(int i = 0; i < primes.size(); ++i){
			test = primes.get(i);
			if(test >= halfNum)
				break;
			if(number % test == 0)
				return false;
		}
		return true;
	}
	
	/**
	 * @param max the largest possible digit that might be a prime. will check all numbers from 0 to this number for being primes
	 */
	public void solve(int max){
		primes.clear();
		int first = primes.size() > 0?primes.lastElement():2;
		for(int i = first; i < max; ++i)
		{
			if(isPrime(i)){
				primes.add(i);
			}
		}
	}
	
	/**
	 * prints the the standard output console.
	 */
	public void printPrimes(){
		for(int i = 0; i < primes.size(); ++i){
			System.out.println(primes.get(i));
		}
		System.out.println(primes.size()+" primes");
	}
	
	public static void main(String[] args){
		Prime p = new Prime();
		long start = System.currentTimeMillis();
		p.solve(200000);
		long end = System.currentTimeMillis();
		p.printPrimes();
		System.out.println((end-start)+" ms");
	}
}
